<template>
    <div>
        <el-table :data="speclist" border style="width: 100%">
            <el-table-column prop="id" label="规格编号" width="180">
            </el-table-column>
            <el-table-column prop="specsname" label="规格名称" width="180">
            </el-table-column>
            <el-table-column label="规格属性">
                <template slot-scope="item">
                    <div>
                        <p v-for="attr in item.row.attrs" :key="attr">{{ attr }}</p>
                    </div>
                </template>
            </el-table-column>
            <el-table-column label="状态">
                <template slot-scope="item">
                    <div>
                        {{ item.row.status == 1 ? '启用' : '禁用' }}
                    </div>
                </template>
            </el-table-column>
            <el-table-column label="操作">
                <template slot-scope="item">
                    <div>
                        <el-button size="mini" @click="edit(item.row.id)" type="primary" plain>编辑</el-button>
                        <el-button @click="del(item.row.id)" size="mini" type="danger" plain>删除</el-button>
                    </div>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination @current-change="changePage" :page-size="pageSize" class="pagin" background
            layout="prev, pager, next" :total="count">
        </el-pagination>
    </div>
</template>

<script>
import { mapGetters, mapActions } from 'vuex'
import { specsDel } from '../../request/api'
export default {
    computed: {
        ...mapGetters({
            speclist: 'spec/getSpecsList',
            pageSize: 'spec/getSize',
            count: 'spec/getCount'
        })
    },
    mounted() {
        this.getSpec()
        this.getCount()

        // console.log(this.getSpec,'123')
        // console.log(this.$store.getters);
    },
    methods: {
        ...mapActions({
            getSpec: 'spec/getSpecsAction',
            getCount: 'spec/getCountAction',
            changePageAction: 'spec/changePageAction'
        }),
        //封装切换页面事件
        changePage(n) {
            this.changePageAction(n)
        },
        //编辑
        edit(id) {
            this.$emit('edit', id)
        },
        //删除
        del(id) {
            this.$confirm("你确定要删除这条数据吗?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(() => {
                    //调用删除接口
                    specsDel({
                        id,
                    }).then((res) => {
                        if (res.code == 200) {
                            this.$message.success(res.msg);
                            //重新调用列表接口
                            this.getSpec()
                            //重新调用总数
                            this.getCount()
                        } else {
                            this.$message.error(res.msg);
                        }
                    });
                })
                .catch(() => {
                    this.$message({
                        type: "info",
                        message: "已取消删除",
                    });
                });
        }
    }
}
</script>

<style scoped>
.pagin {
    float: right;
}
</style>